package com.dj.portal.service.Impl;

import com.dj.portal.dao.PictureMapper;
import com.dj.portal.dao.UserMapper;
import com.dj.portal.pojo.po.Picture;
import com.dj.portal.pojo.po.PictureExample;
import com.dj.portal.pojo.po.User;
import com.dj.portal.service.PictureService;
import com.qf.common.jedis.JedisClient;
import com.qf.common.util.JsonUtils;
import com.qf.common.util.StrKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class PictureServiceImpl implements PictureService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private JedisClient jedisClient;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Picture> getPicturePid(Long pid) {
        //去缓存里查询

        try {
            String json = jedisClient.hget("PICTURELIST",pid + "");
            if (StrKit.notBlank(json)){
                List<Picture> pictureList = JsonUtils.jsonToList(json, Picture.class);
                System.out.println("111111111111111111");
                return pictureList;
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }

        //sql
        PictureExample example = new PictureExample();
        PictureExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Picture> pictureList = pictureMapper.selectByExample(example);

        //存缓存
        try {
            jedisClient.hset("PICTURELIST",pid+"", JsonUtils.objectToJson(pictureList));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }
        System.out.println(2);
        return pictureList;
    }

    @Override
    public List<User> selectByuid(User u) {
        List<User> userList = userMapper.selectByuid(u);
        return userList;
    }

}
